// 2025/10/27
// IPO

class Solution {
public:
    int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
        priority_queue<int> pro_heap;
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> cap_heap;
        for(int i = 0; i < capital.size(); i++)
        {
            cap_heap.emplace(capital[i], i);
        }
        while(k--)
        {
            while(!cap_heap.empty() && cap_heap.top().first <= w)
            {
                auto pr = cap_heap.top();
                cap_heap.pop();
                pro_heap.emplace(profits[pr.second]);
            }
            if(pro_heap.empty())
                break;
            w += pro_heap.top();
            pro_heap.pop();
        }
        return w;
    }
};